#!/usr/bin/python

#Icefilms.info v1.0.10 - anarchintosh / daledude / westcoast13 2011-07-02

#standard module imports
import sys,os
import time,re
import urllib,urllib2,cookielib,base64
import xbmc,xbmcplugin,xbmcgui,xbmcaddon
import unicodedata
import random
import copy

#paths etc need sorting out. do for v1.1.0
icedatapath = 'special://profile/addon_data/plugin.video.icefilms'
metapath = icedatapath+'/mirror_page_meta_cache'
downinfopath = icedatapath+'/downloadinfologs'
transdowninfopath = xbmcpath(downinfopath,'')
transmetapath = xbmcpath(metapath,'')
translatedicedatapath = xbmcpath(icedatapath,'')

	  #Movie Meta Container Strings
          mv_date='9/Feb/2011'          
          mv_db='http://www.megaupload.com/?d=U1RTPGQS'
          mv_base='http://www.megaupload.com/?d=CE07S1EJ'
          mv_db_base_size=230
          mv_additional=''
          mv_additional_size=0

          #TV Meta Container Strings
          tv_date=''
          tv_db=''
          tv_base=''
          tv_db_base_size=0
          tv_additional=''
          tv_additional_size=0

          #Offer to download the metadata
     
          dialog = xbmcgui.Dialog()
          ret = dialog.yesno('Download Meta Containers '+mv_date+' ?', 'There is a metadata container avaliable.','Install it to get images and info for movies.', 'Would you like to get it? Its a large '+str(mv_db_base_size)+'MB download.','Remind me later', 'Install')
          if ret==True:
                 
               #download dem files
               get_db_zip=Zip_DL_and_Install(mv_db,'themoviedb','database')
               get_cover_zip=Zip_DL_and_Install(mv_base,'themoviedb','covers')

               #do nice notification
               if get_db_zip==True and get_cover_zip==True:
                    Notify('small','Metacontainer Installation Success','','')
               elif get_db_zip==False or get_cover_zip==False:
                    Notify('small','Metacontainer Installation Failure','','')


def Zip_DL_and_Install(url,dbtype,installtype):

               #define dl directory
               dlzips=os.path.join(translatedicedatapath,'downloaded meta zips')

               #get the download url
               mu=megaroutines.megaupload(translatedicedatapath)
               print 'URL:',url
               thefile=mu.resolve_megaup(url)

               #define the path to save it to
               filepath=os.path.normpath(os.path.join(dlzips,thefile[1]))
          
               print 'FILEPATH: ',filepath
               filepath_exists=os.path.exists(filepath)
               #if zip does not already exist, download from url, with nice display name.
               if filepath_exists==False:

                    do_wait(thefile[3])
                    
                    print 'downloading zip'
                    
                    Download(thefile[0],filepath,dbtype+' '+installtype)

                    #make a text file with the same name as zip, to act as a very simple download log.
                    textfile=re.sub('.zip','',thefile[1])
                    textfilepath=os.path.join(dlzips,textfile+'.txt')
                    save(textfilepath,' ')
                    
               elif filepath_exists==True:
                    print 'zip already downloaded, attempting extraction'

               print '!!!!handling meta install!!!!'
               mc=metahandlers.MetaContainer()
               install=mc.Install_Icefilms_Container(translatedicedatapath,filepath,dbtype,installtype)
               return install